#coding:utf-8
# User System
import os,ConfigParser,uuid
from tl import *
class UserSystem(object):
    """docstring for UserSystem"""
    def __init__(self,conf_path):
        super(UserSystem, self).__init__()
        self.conf_path = conf_path
        if os.path.isfile("usr.conf"):
            with open('usr.conf',"rb") as f:
                f.write("#UserConfigure")
        else:
            self.cf = ConfigParser.ConfigParser()
            self.cf.read("usr.conf")
        if os.path.isfile("session.conf"):
            with open('session.conf',"rb") as f:
                f.write("#UserConfigure")
        else:
            self.scf = ConfigParser.ConfigParser()
            self.scf.read("session.conf")
    def CheckUser(self,username):
        if username not in self.__UserList():
            return False
        else:
            return True
    def __FlushConfigParser(self,onlyR=False):
            if onlyR:#READ
                self.cf.read("usr.conf")
                self.scf.read("session.conf")
            else:#Write
                self.cf.write(open("usr.conf","w"))
                self.scf.write(open("session.conf","w"))
    def NewUsr(self,username,password,user_path):
        if self.CheckUser(username):
            return False
        else:
            self.cf.set("system",username,password)
            self.cf.add_section(username,"path",user_path)
            self.__FlushConfigParser()
    def DelUsr(self,username,password):
        if self.CheckUser(username)==False:
            #用户名不存在
            return False
        else:
            if self.cf.get("system",username) == str(password):
                self.cf.remove_option("system",username)
                self.cf.remove_section(username)
            else:
                #密码错误
                return False
        self.__FlushConfigParser()
    def Login(self,username,password):
        if self.CheckUser(username)==False:
            #用户名不存在
            return False
        else:
            if self.cf.get("system",username) == str(password):
                return self.__CreateSession(username)
            else:
                #密码错误
                return False
    def Logout(self,session):pass
    def __UserList(self):

        return self.cf.options("system")
    def CheckSession(self):pass
    def VerifySession(self,session):pass
    def CheckConfig(self):
        Users = self.__UserList()
        Sections = self.cf.sections()
        for i in Sections:
            if i in Users:
                Sections.remove(i)
                Users.remove(i)
        if Users != []:
            pass
        if Sections != []:
            for i in Sections:
                self.cf.remove_section(i)
        self.__FlushConfigParser()
    def __CreateSession(self,username,time=1):
        #time默认单位为天
        if self.CheckUser(username)==False:
            #用户不在系统中
            return False
        if username not in self.scf.sections:
            self.scf.add_section(username)
            sessions = Ret_Md5(str(uuid.uuid1()))
            self.scf.set(sessions,username)
            self.__FlushConfigParser()
            return sessions
        else:
            sessions = Ret_Md5(str(uuid.uuid1()))
            self.scf.set(sessions,username)
            self.__FlushConfigParser()
            return sessions

